package com.example.tourpal.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tourpal.MyPlanActivity;
import com.example.tourpal.R;

public class PlanAdapter extends BaseAdapter {
	private static final String UNDER_LINE = "-";

	private List<HashMap<String, Object>> dataSourceList = new ArrayList<HashMap<String, Object>>();
	private LayoutInflater mLayoutInflater;

	private MyPlanActivity activity;

	public final class ItemView {
		public ImageView followSign;
		public TextView planName;
		public TextView planDate;
		public TextView planHint;
		public TextView planDescription;
		public TextView goodCnt;
		public TextView commentCnt;
		public ImageView editBt;
	}

	public PlanAdapter(Context context,
			List<HashMap<String, Object>> dataSourceList) {
		activity = (MyPlanActivity) context;
		this.dataSourceList = dataSourceList;
		mLayoutInflater = LayoutInflater.from(context);
	}

	public void setListItem(List<HashMap<String, Object>> newList) {
		dataSourceList = newList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dataSourceList.size();
	}

	@Override
	public Object getItem(int i) {
		// TODO Auto-generated method stub
		return dataSourceList.get(i);
	}

	@Override
	public long getItemId(int i) {
		// TODO Auto-generated method stub
		return dataSourceList.get(i).hashCode();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewGroup root = null;
		ItemView itemView = null;
		if (convertView == null) {
			itemView = new ItemView();

			convertView = mLayoutInflater.inflate(R.layout.me_plan_item, root);

			itemView.followSign = (ImageView) convertView
					.findViewById(R.id.followSign);
			itemView.planName = (TextView) convertView
					.findViewById(R.id.planName);
			itemView.planDate = (TextView) convertView
					.findViewById(R.id.planDate);
			itemView.planHint = (TextView) convertView
					.findViewById(R.id.planHint);
			itemView.planDescription = (TextView) convertView
					.findViewById(R.id.planDescription);
			itemView.goodCnt = (TextView) convertView.findViewById(R.id.good);
			itemView.commentCnt = (TextView) convertView
					.findViewById(R.id.comment);
			itemView.editBt = (ImageView) convertView
					.findViewById(R.id.editPlan);

			convertView.setTag(itemView);
		} else {
			itemView = (ItemView) convertView.getTag();
		}

		HashMap<String, Object> item = dataSourceList.get(position);

		int followSign = (Integer) item.get("followSign");
		String planName = (String) item.get("planName");
		String planDate = (String) item.get("planDate");
		String planHint = (String) item.get("planHint");
		int goodCnt = (Integer) item.get("goodCnt");
		int commentCnt = (Integer) item.get("commentCnt");

		if (followSign == 1) {
			itemView.followSign
					.setBackgroundResource(R.drawable.recruit_sign_middle);
		}

		itemView.planName.setText(planName);
		itemView.planDate.setText(planDate);
		int end = planHint.lastIndexOf(UNDER_LINE);
		itemView.planHint.setText(planHint.substring(0, end));
		String[] hintList = planHint.split(UNDER_LINE);
		itemView.planDescription.setText(hintList[hintList.length - 1]);
		itemView.goodCnt.setText(String.valueOf(goodCnt));
		itemView.commentCnt.setText(String.valueOf(commentCnt));
		itemView.editBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				activity.editPlan(position);
			}
		});

		return convertView;
	}

}
